package edu.geo.utils;

import java.io.File;
import java.net.URL;

/**
 * Esta clase permite obtener el directorio donde se encuentra la clase, a partir de su nombre.
 * 
 * @author alejandro.sanchez
 *
 */
public class ResourceLocator {
	
	
	public static String getFullPath(Class<?> aClass)
	{
		String nombre =  aClass.getSimpleName().concat(".class");
		URL url = aClass.getResource(nombre);
        if ( url != null )
		{
        	String path = url.toString();
        	if (path.startsWith ("file:/"))
    		{
        		path = path.substring (6);
    		}
        	else if(path.startsWith ("/"))
        	{
        		path = path.substring (1);
        	} 
        	path = path.substring(0, path.length() - nombre.length() - 1 );
        	return path;
		}
        return null;
	}
	
	public static String getBasePath(String archivo)
	{
		ClassLoader.getSystemClassLoader();
		URL url = ClassLoader.getSystemResource(archivo);
		String path = null;
		if (url != null)
		{
			File fileUrl = new File(url.getPath());
			path = fileUrl.getParent();
		}
		return 	path;
	}
	
	public static String getBasePath(Class<?> aClass)
	{
		String name =  aClass.getSimpleName().concat(".class");
		String fullName = aClass.getName();
		fullName = fullName.replace('.', System.getProperty("file.separator").charAt(0)).concat(".class");
		URL url = aClass.getResource(name);
		String path = null;
		if ( url != null )
		{
        	if (url != null)
    		{
    			File fileUrl = new File(url.getPath());
    			path = fileUrl.getParent();
    		}
        	path = url.toString();        	
        	if (path.startsWith ("file:/"))
    		{
        		path = path.substring (6);
    		}
        	else if(path.startsWith ("/"))
        	{
        		path = path.substring (1);
        	}  
        	path = path.substring(0, path.length() - fullName.length() - 1 );
        	return path;
		}
        return null;
	}	
}
